package san.service.m;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.utils.Page;

import san.dao.m.ProductDao;
import san.dao.m.SupplierDao;
import san.dao.m.VarietyDao;
import san.entity.m.Product;
import san.entity.m.Supplier;
import san.entity.m.Variety;

@Service
@Transactional
public class ProductService {
	/**
	 * 自动生成ProductService类.
	 */
	private static final long serialVersionUID = 5945114496687830925L;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private VarietyDao varietyDao;
	@Autowired
	private SupplierDao supplierDao;

	@Transactional(readOnly = true)
	public Page<Product> getAll(Page<Product> page) throws Exception {
		return productDao.getAll(page);
	}
	
	@Transactional(readOnly = true)
	public List<Product> getAll() throws Exception {
		return productDao.getAll();
	}
	
	@Transactional(readOnly = true)
	public Product get(String id) throws Exception {
		return productDao.get(id);
	}

	public void save(Product product) throws Exception {
		Supplier supplier = supplierDao.get(product.getSupplier()
				.getId());
		Variety variety = varietyDao.get(product.getVariety().getId());
		product.setSupplier(supplier);
		variety.addProduct(product);
		varietyDao.save(variety);
	}

	public void delete(String id) throws Exception {
		productDao.delete(id);
	}

}
